package com.open.system.dispatch.impl;

import com.open.base.utils.UserHolderUtils;
import com.open.system.dispatch.IRoleDispatch;
import com.open.common.model.Page;
import com.open.base.model.Role;
import com.open.system.model.UserRole;
import com.open.system.model.dto.RoleDto;
import com.open.system.service.IRoleService;
import com.open.system.service.IUserRoleService;
import com.open.common.utils.CollectionUtils;
import com.open.common.utils.StringUtils;
import com.open.common.utils.ThrowUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 角色信息
 *
 * @author 大树03
 * @date 2025-02-01 22:55
 */
@Component(value = "roleDispatch")
@Slf4j
public class RoleDispatchImpl implements IRoleDispatch {

    @Autowired
    private IUserRoleService userRoleService;
    
    private IRoleService roleService;

    @Autowired
    public void setRoleService(IRoleService roleService) {
        this.roleService = roleService;
    }

    @Override
    public Boolean save(Role model) {
        return roleService.save(model);
    }

    @Override
    public Boolean update(Role model) {
        //ThrowUtils.throwIf(model.getId() == 1 && model.getStatus() == 1, "不允许停用超级管理员");
        return roleService.update(model);
    }

    @Override
    public Boolean batchSave(List<Role> models) {
        return roleService.batchSave(models);
    }

    @Override
    public Boolean batchUpdate(List<Role> models) {
        return roleService.batchUpdate(models);
    }

    @Override
    public Page<Role> findPage(Page<Role> page, Role item) {
        return roleService.findPage(page, item);
    }

    @Override
    public Role findByPk(Long pk) {
        return roleService.findByPk(pk);
    }

    @Override
    public List<Role> findByModel(Role model) {
        return roleService.findByModel(model);
    }

    @Override
    public Boolean remove(List<Long> pks) {
        ThrowUtils.throwIf(pks.stream().anyMatch(id -> id == 1), "不允许删除超级管理员");
        return roleService.remove(pks);
    }

    @Override
    public List<Long> getRoleIds(Long userId) {
        if (userId == null) {
            return Collections.emptyList();
        }
        List<UserRole> lists = userRoleService.findByUserId(userId);
        return lists.stream().map(UserRole::getRoleId).collect(Collectors.toList());
    }

    @Override
    public Long getRoleId(Long userId) {
        if (userId == null) {
            return null;
        }
        List<UserRole> lists = userRoleService.findByUserId(userId);
        if (CollectionUtils.isNotEmpty(lists)) {
            for (UserRole userRole : lists) {
                if (StringUtils.isNotNull(userRole.getStatus()) && userRole.getStatus() == 0) {
                    return userRole.getRoleId();
                }
            }
            //默认返回第一个角色
            return lists.get(0).getRoleId();
        }
        return null;
    }

    @Override
    public List<Role> findByUserId(Long userId) {
        return roleService.findByUserId(userId);
    }

    @Override
    public Map<String, List<Role>> getRoleMap(Long id) {
        Map<String, List<Role>> map = new HashMap<>();
        List<Role> assignList = roleService.findByUserId(id);
        List<Role> allList = roleService.findByModel(new Role(0));
        map.put("assignRoles", assignList);
        map.put("allRolesList", allList);
        return map;
    }

    @Override
    public Boolean doAssignRole(RoleDto roleDto) {
        userRoleService.removeByUserId(Collections.singletonList(roleDto.getUserId()));
        if (CollectionUtils.isNotEmpty(roleDto.getRoleIdList())) {
            for (Long roleId : roleDto.getRoleIdList()) {
                UserRole userRole = new UserRole();
                userRole.setUserId(roleDto.getUserId());
                userRole.setRoleId(roleId);
                userRoleService.save(userRole);
            }
        }
        return null;
    }

    @Override
    public Boolean changeRole(Long roleId) {
        userRoleService.findByUserId(UserHolderUtils.getUserId()).forEach(userRole -> {
            userRole.setStatus(userRole.getRoleId().equals(roleId) ? 0 : 1);
            userRoleService.update(userRole);
        });
        return true;
    }
}